package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.ProductType;
import net.wanho.service.ProductTypeService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/productCategory")
@RequiredArgsConstructor
public class ProductCategoryController {
    private final ProductTypeService productTypeService;

    /**
     *  查询所有商品分类
     */
    @GetMapping("/product-category-tree")
    public AjaxResult productCategoryTree() {
        return AjaxResult.success(productTypeService.productCategoryTree());
    }

    /**
     *  校验分类编码是否已存在
     */
    @GetMapping("/verify-type-code")
    public AjaxResult verifyTypeCode(String typeCode) {
        return AjaxResult.success(productTypeService.lambdaQuery().eq(ProductType::getTypeCode, typeCode).count() <= 0);
    }

    /**
     *  添加分类
     */
    @PostMapping("/type-add")
    public AjaxResult addProductType(@RequestBody ProductType productType) {
        productTypeService.insertProductType(productType);
        return AjaxResult.success(null, "分类添加成功！");
    }

    /**
     *  根据Id查询分类
     */
    @GetMapping("/{id}")
    public AjaxResult getProductTypeById(@PathVariable Integer id) {
        return AjaxResult.success(productTypeService.getById(id));
    }

    /**
     *  编辑分类
     */
    @PutMapping("/type-update")
    public AjaxResult updateProductType(@RequestBody ProductType productType) {
        productTypeService.save(productType);
        return AjaxResult.success(null, "分类修改成功！");
    }

    /**
     *  删除分类
     */
    @DeleteMapping("/type-delete/{id}")
    public AjaxResult deleteProductType(@PathVariable Integer id) {
        productTypeService.removeById(id);
        return AjaxResult.success(null, "分类删除成功！");
    }
}
